// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html
import hall from "../scenes/hall";

const { ccclass, property } = cc._decorator;
@ccclass
export default class AccelerometerMgr {

    static AccelerFun:(res)=>void = null;

    static VivoAccFunes:Function[]=[]

    /**
    * 
    * @param AccFun(res) 加速度计变化时的运行逻辑  res ：{x,y,z}
    */
    public static StartAccelerometer(AccFun?: (res) => void) {
        console.log("调用了");
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME_SUB:
            case cc.sys.WECHAT_GAME: {

                // if(cc.s)
                this.VivoAccFunes.push(AccFun);
                window["wx"].startAccelerometer(
                    {
                        interval: 'game',
                        success() { console.log("调用加速度计成功！") },
                        fail() { console.log("加速度计调用失败！") },
                        complete() { console.log("尝试调用加速度计！") }
                    });
                window["wx"].onAccelerometerChange((res) => {
                    let funes = this.VivoAccFunes;

                    let dir = null;
                    // console.log("????")
                    for (let index = 0; index < funes.length; index++) {
                        const element = funes[index];
                        
                        if(hall.PlatFrom=="ios"){
                            dir = res;
                            dir.x *=-1;
                            dir.y *=-1;
                            dir.z *= -1;
                        }else{
                            dir = res.result;
                        }
                        element && element(dir);
                    }
                });
                break;
            }
            case cc.sys.BYTEDANCE_GAME:
            case cc.sys.BYTEDANCE_GAME_SUB: {
                window["tt"].startAccelerometer(
                    {
                        success(res) { console.log(res.errMsg, "加速度计调用成功！"); },
                        fail(res) { console.log(res.errMsg, "加速度计调用失败！"); },
                        complete() { console.log("尝试调用加速度计！"); }
                    });
                window["tt"].onAccelerometerChange((res) => {
                    // console.warn(res);
                    AccFun && AccFun({x:-res.x*10,y:-res.y*10,z:-res.z*10});
                });
                break;
            }
            case cc.sys.VIVO_GAME: {
                window['qg'].subscribeCompass({
                    callback: function (res) {
                        AccFun && AccFun(res);
                    }
                });
                break;
            }
            case cc.sys.OPPO_GAME: {
                window["qg"].startAccelerometer(
                    {
                        success(res) { console.log(res.errMsg, "加速度计调用成功！"); },
                        fail(res) { console.log(res.errMsg, "加速度计调用失败！"); },
                        complete() { console.log("尝试调用加速度计！"); }
                    });
                window["qg"].onAccelerometerChange((res) => {
                    AccFun && AccFun(res)
                });
                break;
            }
            default: {
                if (cc.sys.isNative) {
                    
                } else {
                    if (cc.sys.platform == 3 || cc.sys.platform == 4) {
                        window['kwaigame'].startDeviceMotionListening(
                            {
                                interval:"game",
                                success(res) { console.log(res.errMsg, "加速度计调用成功！"); },
                                fail(res) { console.log(res.errMsg, "加速度计调用失败！"); },
                                complete() { console.log("尝试调用加速度计！"); }
                            });
                        window['kwaigame'].onDeviceMotionChange((res)=>
                        {
                            let r:any = {};
                            r.x = res.alpha;
                            r.y = res.beta;
                            r.z = res.gamma;
                            AccFun&&AccFun(r);
                        });
                    } else {
                        this.AccelerFun = (res)=>
                        {
                            let r =res.accelerationIncludingGravity;
                            AccFun(r);
                        }
                        //devicemotion
                        //deviceorientation
                        window.addEventListener('devicemotion',this.AccelerFun, false);
                    }
                }
                break;
            }
        }
    }

    public static StopAccelerometerFun(){
        
    }

    public static StopAccelerometer() {
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME_SUB:
            case cc.sys.WECHAT_GAME: {
                // 微信
                window["wx"].stopAccelerometer(
                    {
                        success() { console.log("停用加速度计成功！") },
                        fail() { console.log("停用加速度计失败！") },
                        complete() { console.log("尝试停用加速度计") }
                    });

                this.VivoAccFunes=[]
                break;
            }
            case cc.sys.BYTEDANCE_GAME:
            case cc.sys.BYTEDANCE_GAME_SUB: {
                //头条
                window["tt"].stopAccelerometer(
                    {
                        success() { console.log("停用加速度计成功！") },
                        fail() { console.log("停用加速度计失败！") },
                        complete() { console.log("尝试停用加速度计") }
                    });
                break;
            }
            case cc.sys.VIVO_GAME: {
                window['qg'].subscribeCompass({
                    callback: function (data) {
                        console.log("停用加速度计")
                    }
                })
                break;
            }
            case cc.sys.OPPO_GAME: {
                window['qg'].stopAccelerometer({
                    success() { console.log("停用加速度计成功！") },
                    fail() { console.log("停用加速度计失败！") },
                    complete() { console.log("尝试停用加速度计") }
                })
                break;
            }
            default: {
                if (cc.sys.isNative) {

                } else {
                    if (cc.sys.platform == 3 || cc.sys.platform == 4) {
                        //TODO 快手加速计  由于快手没有加速计这里使用的是获取设备方向接口
                        window["kwaigame"].stopDeviceMotionListening();
                    } else {
                        window.removeEventListener("devicemotion", this.AccelerFun, false);
                    }
                }
                break;
            }
        }
    }
}
